package com.niit.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

object Spark_SQL_Base01 {


  def main(args: Array[String]): Unit = {
      //准备环境
      val sparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkSQL")
      val spark = SparkSession.builder().config(sparkConf).getOrCreate();

      //DataFrame
     val df = spark.read.json("input/user.json")
     df.show()
    //DataFrame-->SQL
    df.createOrReplaceTempView("user")
    spark.sql("select * from user").show()
    //查询年龄平均值
    spark.sql("select avg(age) from user").show()

    //DataFrame-->DSL
    //使用DSL语法查询姓名
    df.select("username").show
    //查询年龄+1
    //在DataFrame中，如果涉及转换操作，需要引入转换规则
    import spark.implicits._
    df.select($"age" +1).show
    df.select('age + 1 as "newAge").show()

    //RDD <==> DataFrame
    val rdd = spark.sparkContext.makeRDD( List( (1,"zhangsan",30) , (2,"lisi",40)  ) )
    val df1  = rdd.toDF("id","name","age")
    df1.show()
    val rdd1 =  df1.rdd


    //DataFrame <==> DataSet  两者区别在于类型
    val ds = df1.as[User]  //DataFrame + 类型  = DataSet
    val df2 =  ds.toDF()


    //RDD <==> DataSet
    val ds2 = rdd.map{
      case (id,name,age) =>{
        User(id,name,age)
      }
    }.toDS()
      //DataSet里面包含了 rdd 所有直接通过属性的方式调用出来
    val rdd2 = ds2.rdd



      //关闭spark
     spark.close()
  }

  case class User(id:Int,name:String,age:Int)

}
